package com.ycb.bookserver.service.impl;

import com.book.constant.MessageConstant;
import com.book.exception.AccountNotFoundException;
import com.book.exception.PasswordErrorException;
import com.ycb.book.dto.EmployeeLoginDTO;
import com.ycb.book.entity.Employee;
import com.ycb.bookserver.mapper.EmployMapper;
import com.ycb.bookserver.service.EmployService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class EmployServiceImpl implements EmployService {
    @Autowired
    private EmployMapper employMapper;
    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
       Employee employee = employMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对

        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }



        //3、返回实体对象
        return employee;
    }
}
